package cn.attaching.security;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Message Digest Algorithm 加密算法的基本实现
 * 高杰创建于 2017/01/19.
 */
public class MdSecurity {

    /**
     * MD5 加密
     * @param src 要加密的字符串
     * @return 加密完成的密文
     * @throws Exception
     */
    public static String getMd5(String src) throws Exception {
        if(src == null || src.length() == 0) return "";
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = src.getBytes(Charset.forName("UTF-8"));
            md5.update(bytes);
            byte[] after = md5.digest();
            return TypeConversion.bytesToHexString(after);
        } catch (NoSuchAlgorithmException e) {
            throw new Exception("无法找到加密方法", e);
        }
    }
}
